<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <div th:replace="header::common_resource"></div>
</head>
<body>
    <form id="formId" class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">商品名</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" placeholder="请输入商品名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">子标题</label>
            <div class="layui-input-block">
                <input type="text" name="subtitle" autocomplete="off" placeholder="请输入子标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品分类</label>
            <div class="layui-input-inline">
                <select name="parentId" id="TopCategoryId" lay-filter="TopCategoryId">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="categoryId" id="SecondCategoryId" lay-filter="SecondCategoryId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-block">
                <input type="text" name="price" autocomplete="off" placeholder="请输入价格" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存</label>
            <div class="layui-input-block">
                <input type="text" name="stock" autocomplete="off" placeholder="请输入库存" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" th:title="在售" checked/>
                <input type="radio" name="status" value="0" th:title="下架"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品图片</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img width="250px" height="250px" class="layui-upload-img" id="demo1">
                        <input type="hidden" name="mainImage" id="imageId">
                        <p id="demoText"></p>
                    </div>
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品详情</label>
            <div class="layui-input-block">
                <textarea id="content" name="detail" placeholder="请输入详情" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" onclick="submitForm()" class="layui-btn" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script th:src="@{/kindeditor/kindeditor.js}"></script>
    <script>

        $(function () {
            $.post(
                '/category/selectTopCategory',
                function (result) {
                    $(result.data).each(function () {
                        $('#TopCategoryId').append('<option value="' + this.id + '">' + this.name + '</option>');
                    });
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render('select');
                    });
                },
                'json'
            );
        });

        layui.use(['form'], function () {
            var form = layui.form;
            form.on('select(TopCategoryId)', function (data) {
                $('#SecondCategoryId option:gt(0)').remove();
                var parentId = $('#TopCategoryId').val();
                console.log(parentId);
                $.post(
                    '/category/selectSecondCategory',
                    {'parentId' : parentId},
                    function (result) {
                        console.log(result);
                        $(result.data).each(function () {
                            $('#SecondCategoryId').append('<option value="' + this.id + '">' + this.name + '</option>');
                        });
                        form.render('select');
                    },
                    'json'
                );
            });
        });

        KindEditor.ready(function() {
            var kindEditorParams = {
                afterBlur: function() {
                    this.sync();
                }
            };

            KindEditor.create('#content', kindEditorParams);
        });

        layui.use(['upload'], function() {
            var upload = layui.upload;

            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                ,url: '/upload/uploadImage' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(result){
                    if (result.code == 0) {
                        console.log(result.data);
                        console.log($('#formId').serialize());
                        $('#imageId').val(result.data);
                        $('#demoText').html(''); //置空上传失败的状态
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

        });


        function submitForm() {
            $.post(
                '/product/insert',
                $('#formId').serialize(),
                function(result) {
                    console.log(result);
                    if (result.ok) {
                        mylayer.okMsg(result.msg)
                        setInterval(function() {
                            //关闭弹出框
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //刷新父页面
                            window.parent.location.reload();
                        }, 2000)
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>